package com.iteaj.izone.ark;

import com.alipay.sofa.ark.spi.model.Biz;
import com.alipay.sofa.ark.spi.service.classloader.ClassLoaderHook;
import com.alipay.sofa.ark.spi.service.classloader.ClassLoaderService;
import com.alipay.sofa.ark.spi.service.extension.Extension;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;

/**
 * create time: 2020/3/31
 *
 * @author iteaj
 * @since 1.0
 */
@Extension("biz-classloader-hook")
public class IzoneBizClassLoaderHook implements ClassLoaderHook<Biz> {

//    private LibUrlClassLoader libUrlClassLoader;

    @Override
    public Class<?> preFindClass(String name, ClassLoaderService classLoaderService, Biz biz) throws ClassNotFoundException {
        return null;
    }

    /**
     * 先从biz和插件到处的 classpath查找, 没找到在到通用类库查找
     * @param name
     * @param classLoaderService
     * @param biz
     * @return
     * @throws ClassNotFoundException
     */
    @Override
    public Class<?> postFindClass(String name, ClassLoaderService classLoaderService, Biz biz) throws ClassNotFoundException {
        Class<?> aClass = null;
//        try {
//            if(libUrlClassLoader == null) {
//                String mainClass = biz.getMainClass();
//                ((URLClassLoader)biz.getBizClassLoader()).getURLs()
//                Class<?> mainClazz = biz.getBizClassLoader().loadClass(mainClass);
//                if(WebApplication.class.isAssignableFrom(mainClazz)) {
//                    libUrlClassLoader = LibUrlClassLoader.getWebInstance();
//                } else {
//                    libUrlClassLoader = LibUrlClassLoader.getInstance();
//                }
//            }
//
//            aClass = libUrlClassLoader.loadClass(name);
//        } catch (ClassNotFoundException e) {
//            /*ignore error*/
//        }
        return aClass;
    }

    @Override
    public URL preFindResource(String name, ClassLoaderService classLoaderService, Biz biz) {
        return null;
    }

    @Override
    public URL postFindResource(String name, ClassLoaderService classLoaderService, Biz biz) {
//        return libUrlClassLoader.findResource(name);
        return null;
    }

    @Override
    public Enumeration<URL> preFindResources(String name, ClassLoaderService classLoaderService, Biz biz) throws IOException {
        return null;
    }

    @Override
    public Enumeration<URL> postFindResources(String name, ClassLoaderService classLoaderService, Biz biz) throws IOException {
//        return libUrlClassLoader.findResources(name);
        return null;
    }
}
